#include <iostream>
#include "screen.h"
//#include "windows.h"

using namespace std;
int Tower[3];
// Ham dung de dieu khien mau bang so
void setcolor(unsigned short color)                
{                                                  
    HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hcon,color);
}
void DrawTower(int n)
{
	int i,j;
	for (i=0;i<10;i++)
	{
		setcolor(14);
		gotoxy(15,5+i);
		cout<<char(186);
		gotoxy(30,5+i);
		cout<<char(186);
		gotoxy(45,5+i);
		cout<<char(186);
	}
	for (i=0;i<n;i++)
	{
		for (j=0;j<2*i+1;j++)
		{
			gotoxy(15-i+j,15-n+i);
			setcolor((i+5)%15);
			cout<<char(219);
		}
	}
}
// Chuyen khoi i tu cot a qua cot b
void MoveDish(int a,int b,int i,int n)
{
	i--;
	int j,k;
	for (j=15-Tower[a-1];j>2;j--)
	{
		for (k=0;k<2*i+1;k++)
		{
			gotoxy(15*a-i+k,j);
			cout<<" ";
		}
		if (j>=5)
		{
			gotoxy(15*a,j);
			setcolor(14);
			cout<<char(186);
		}
		for (k=0;k<2*i+1;k++)
		{
			gotoxy(15*a-i+k,j-1);
			setcolor((i+5)%15);
			cout<<char(219);
		}
		delay(20);
	}
	if (a<b)
	{
		for (j=15*a;j<=15*b;j++)
		{
			for (k=0;k<2*i+1;k++)
			{
				gotoxy(j-i+k,2);
				setcolor((i+5)%15);
				cout<<char(219);
			}
			delay(10);
			for (k=0;k<2*i+1;k++)
			{
				gotoxy(j-i+k,2);
				cout<<" ";
			}
		}
	}
	else
	{
		for (j=15*a;j>=15*b;j--)
		{
			for (k=0;k<2*i+1;k++)
			{
				gotoxy(j-i+k,2);
				setcolor((i+5)%15);
				cout<<char(219);
			}
			delay(10);
			for (k=0;k<2*i+1;k++)
			{
				gotoxy(j-i+k,2);
				cout<<" ";
			}	
		}
	}
	for (j=2;j<15-Tower[b-1]-1;j++)
	{
		for (k=0;k<2*i+1;k++)
		{
			gotoxy(15*b-i+k,j);
			cout<<" ";
		}
		if (j>=5)
		{
			gotoxy(15*b,j);
			setcolor(14);
			cout<<char(186);
		}
		for (k=0;k<2*i+1;k++)
		{
			gotoxy(15*b-i+k,j+1);
			setcolor((i+5)%15);
			cout<<char(219);
		}
		delay(10);
	}
	Tower[a-1]--;
	Tower[b-1]++;
}

void MoveTower(int a,int c,int b,int m,int n)
{
	if (m<1)
		return;
	else
	{
		if (m==1)
			MoveDish(a,c,1,n);
		else
		{
			MoveTower(a,b,c,m-1,n);
			MoveDish(a,c,m,n);
			MoveTower(b,c,a,m-1,n);
		}
	}
}

int main()
{
	int n;
	cout<<"Nhap so dia: ";
	cin>>n;
	system("cls");
	DrawTower(n);
	Tower[0]=n;
	Tower[1]=0;
	Tower[2]=0;
	gotoxy(1,20);
	cout<<"Nhan phim bat ki de tiep tuc";
	getch();
	gotoxy(1,20);
	cout<<"                            ";
	MoveTower(1,3,2,n,n);
	gotoxy(1,20);
	system("pause");
	return 0;
}